<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house" style="font-weight: bold">&nbsp;&nbsp;后台管理</i>
        </el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-claim">&nbsp;&nbsp;采购管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-circle-plus">&nbsp;&nbsp;采购申请添加</i></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div>
      <div class="data-canRead" style="width: 100%;background-color: white;height: auto">
        <el-divider content-position="left">数据添加</el-divider>
        <el-form
            :model="applicationForm"
            :rules="rules"
            ref="applicationForm"
            label-width="100px"
            style="margin-right: 25px">
          <el-form-item label="采购单位:" prop="companyName">
            <el-input v-model="applicationForm.companyName" placeholder="请输入采购单位"></el-input>
          </el-form-item>
          <el-form-item label="订单金额:" prop="amount">
            <el-input v-model="applicationForm.amount" placeholder="输入订单金额"></el-input>
          </el-form-item>
          <el-form-item label="采购编号:" prop="cargoNumber">
            <el-input v-model="applicationForm.cargoNumber" placeholder="输入采购编号"></el-input>
          </el-form-item>
          <el-form-item label="交票日期:" prop="deliveryDate">
            <el-date-picker
                v-model="applicationForm.deliveryDate"
                type="date"
                placeholder="选择交票日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('applicationForm')" style="color: white">添加</el-button>
            <el-button type="warning" @click="resetForm('applicationForm')" style="color: white">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      applicationForm: {
        companyName: '联想',
        amount: 23000,
        cargoNumber: 523572648,
        deliveryDate: '2022-10-12',
      },
      rules: {
        amount: [
          {required: true, message: "请输入订单金额", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        cargoNumber: [
          {required: true, message: "请输入采购货单编号", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        companyName: [
          {required: true, message: "请输入设备名称", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        deliveryDate: [
          {required: true, message: "请选择交票日期", trigger: 'change'}
        ],
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9080/purchaseApplications/add-new';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.applicationForm);
          console.log('formData = ' + formData);

          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            console.log(responseBody);
            if (responseBody.state == 20000) {
              this.$message({
                showClose: true,
                message: '添加成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message({
                showClose: true,
                message: responseBody.message,
                type: 'error'
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }

  }
}

</script>


<style scoped>
* {
  color: black;
}

.data-canRead {
  width: 380px;
  height: 380px;
  margin-top: 15px;
  box-shadow: 0px 5px 5px #8d858a;
  float: left;
  margin-right: 16px;
}

</style>